package com.leo.springbootmultithreadbasic.exceptionhandle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Leo
 * @version V1.0
 */
public class MainDemo {

    private static final Logger logger = LoggerFactory.getLogger(MainDemo.class);

    public static void main(String[] args) {
//        test1();
        test2();
    }


    /**
     * 测试场景：为一个线程指定异常处理器
     */
    public static void test1() {
        Thread thread = new MyThread();
        thread.setName("Thread 1");
        thread.setUncaughtExceptionHandler((t, e) -> logger.error("{} 出现了异常, 异常为：{}", t.getName(), e));
        thread.start();
    }

    /**
     * 测试场景: 设置通用异常处理器
     */
    public static void test2() {

        MyThread.setDefaultUncaughtExceptionHandler((t, e) -> logger.error("{} 出现了异常, 异常为：{}", t.getName(), e));

        Thread thread = new MyThread();
        thread.setName("Thread 1");
        thread.start();

        Thread thread2 = new MyThread();
        thread2.setName("Thread 2");
        thread2.start();
    }
}
